<template>
    <div class="con">
        <div class="toutu">
            <img src="../assets/images/loginbg.png" alt>
        </div>
        <div class="info">
            <dl>
                <dd>账号</dd>
                <dt>
                    <input
                        type="text"
                        placeholder="请输入手机号"
                        pattern="[0-9]*"
                        v-model="phone"
                        oninput="if(this.value.length>11) this.value = this.value.slice(0,11);"
                    >
                </dt>
            </dl>
            <dl>
                <dd>密码</dd>
                <dt>
                    <input type="password" placeholder="请输入密码" v-model="password">
                </dt>
            </dl>
        </div>
        <div class="btn btn1" @click="login()">
            <button>登录</button>
        </div>
        <div class="btn btn2" @click="register()">
            <button>注册</button>
        </div>
        <p @click="forgetpwd()">忘记密码</p>
    </div>
</template>

<script>
export default {
    name: "login",
    data() {
        return {
            phone: "",
            password: "",
            timestamp:""
        };
    },
    methods: {
        register() {
            this.$router.push({ name: "register" });
        },
         forgetpwd() {
            this.$router.push({ name: "forgetpwd" });
        },
        login() {
            var myreg = /^[1][0-9]{10}$/;
            if (this.phone == "") {
                this.$toast("请输入手机号");
            } else if (!myreg.test(this.phone)) {
                this.$toast("请填写正确的手机号");
            } else if (this.password == "") {
                this.$toast("请输入密码");
            } else {
                var md5pwd = this.md5(
                    `${this.md5(this.password)}${this.password}`
                );
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/app/login/bssUser/login?appid=web.2019&format=json&timestamp=${
                        this.timestamp
                    }`,
                    data: {
                        loginName: this.phone,
                        password: md5pwd,
                        loginType: 1
                    }
                })
                    .then(response => {
                        console.log(response);
                        if (response.data.success == 1) {
                            //登录客户的首页
                            this.$toast("登录成功");
                            this.$router.push({ name: "home" });

                            localStorage.setItem("token",response.data.data.token)
                            localStorage.setItem("userId",response.data.data.id)
                            localStorage.setItem("loginphone",this.phone)


                        } else {
                            this.$toast(response.data.message);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        this.$toast("连接服务器异常");
                    });
            }
        }
    },
    mounted(){
         this.timestamp = Date.parse(new Date());
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    width: 100%;
    min-height: 100vh;
    background: #fff;
    overflow: hidden;
}
.con .toutu {
    width: 100%;
}
.con .toutu img {
    width: 100%;
}
.con .info {
    width: 90%;
    margin-left: 5%;
}
.con .info dd {
    height: 40px;
    line-height: 40px;
}
dl {
    margin-top: 20px;
}
.con .info dt {
    height: 40px;
    line-height: 40px;
    border-bottom: 1px solid #ececec;
    width: 100%;
}
.con .info input {
    width: 100%;
}
.btn1 {
    margin-top: 14px;
}
.btn {
    width: 90%;
    margin: 0 auto;
    height: 48px;
    line-height: 48px;
    background: #ffae4e;
    margin-top: 30px;
    text-align: center;
    border-radius: 25px;
    box-shadow: 0px 10px 30px 0px #fbc27f;
}
.btn button {
    width: 100%;
    font-size: 17px;
    color: #fff;
    background: transparent;
}
p {
    text-align: center;
    margin-top: 40px;
    color: #ffae4e;
}
</style>
